package com.liu.system.manage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.liu.common.domain.ResultVo;
import com.liu.common.utils.JacksonUtil;
import com.liu.system.manage.domain.DRole;
import com.liu.system.manage.service.IRoleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

@RestController
@RefreshScope
@RequestMapping("/vue-admin-template/role")
@Slf4j
@CrossOrigin
public class RoleController {

    @Autowired
    IRoleService roleService;


    /**
     * 获取分页查询用户列表
     * @param query
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/list")
    public ResultVo list(String query, int page, int limit){
        Map map = Maps.newHashMap();
        IPage<DRole> pages = roleService.queryUserForPage(page,limit,query);
        map.put("total",pages.getTotal());
        map.put("items",pages.getRecords());
        return  ResultVo.success(map);
    }


    /**
     *
     * @param role
     * @return
     */
    @PostMapping
    public ResultVo addRole(@RequestBody DRole role){

        log.info(JacksonUtil.printJson(role));


        roleService.addRole(role);

        return  ResultVo.success("角色添加成功！！");
    }


    /**
     * 更新角色
     * @param role
     * @return
     */
    @PutMapping
    public ResultVo updateRole(@RequestBody DRole role){

        roleService.updateRole(role);

        return  ResultVo.success("角色更新成功！！");
    }


    /**
     * 更新角色
     * @param role
     * @return
     */
    @DeleteMapping
    public ResultVo deleteRole(@RequestBody DRole role){

        roleService.delete(role.getId());

        return  ResultVo.success("角色删除成功！！");
    }

}
